////
//
/// \file ./test/ws_parser_test.cpp
//
//  Copyright 2007 Chandler Carruth
//
//  Licensed under the Apache License, Version 2.0 (the "License"); you may not
//  use this file except in compliance with the License.  You may obtain a copy
//  of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
//  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
//  License for the specific language governing permissions and limitations
//  under the License.
//
////

#include <inc/parser/ws_parser.hpp>

#include <string>

#include <gtest/gtest.h>

#include <inc/parser/utilities.hpp>

namespace inc {
namespace parser {

using std::string;

class WSParserTest : public testing::Test
{
 public:
  WSParser ws_parser;
};

TEST_F(WSParserTest, test_ws_parse)
{
  const string data[] = { "   foo", "\t\t\tfoo", "  \t foo",
                          " \n\nfoo", "\n\n foo", " \n\tfoo", "\t\n foo" };
  for( int i = 0 ; i < elementsof(data) ; ++i )
  {
    string::const_iterator last_parsed;
    EXPECT_TRUE( ws_parser.parse_ws( data[i].begin(),
                                     data[i].end(),
                                     last_parsed ) )
        << "Failed to parse: '" << data[i] << "'";
    EXPECT_EQ( data[i].substr( 0, data[i].size() - 3 ),
               string( data[i].begin(), last_parsed ) );
  }
}

TEST_F(WSParserTest, test_strict_ws_parse)
{
  const string data[] = { "   foo", "\t\t\tfoo", "  \t foo",
                          "   \n\n\n", "  \n \n" };
  for( int i = 0 ; i < elementsof(data) ; ++i )
  {
    string::const_iterator last_parsed;
    EXPECT_TRUE( ws_parser.parse_strict_ws( data[i].begin(),
                                            data[i].end(),
                                            last_parsed ) )
        << "Failed to parse: '" << data[i] << "'";
    EXPECT_EQ( data[i].substr( 0, data[i].size() - 3 ),
               string( data[i].begin(), last_parsed ) );
  }
}

TEST_F(WSParserTest, test_eol_parse)
{
  const string data[] = { "\nfoo", "  \n  foo", " \t \n \t\tfoo",
                          "   \n\n\nfoo", "  \n \nfoo", "\t\n\n \t\n  foo" };
  for( int i = 0 ; i < elementsof(data) ; ++i )
  {
    string::const_iterator last_parsed;
    EXPECT_TRUE( ws_parser.parse_eol_ws( data[i].begin(),
                                         data[i].end(),
                                         last_parsed ) )
        << "Failed to parse: '" << data[i] << "'";
    EXPECT_EQ( data[i].substr( 0, data[i].size() - 3 ),
               string( data[i].begin(), last_parsed ) );
  }
}

} //  end parser namespace
} //  end inc namespace
